var toUTF8 = require('./ToUTF8');
var pool = require('./DBConnectPool');
var logger = require('../../common/Logger/Logger');
var redisTool = require('../../common/utils/redistools.js');


var key = 'AccessToken1';

module.exports = {
    setAccessToken: function (data, callBack) {
        // setAccessToken:function (key,data,callBack) {
        redisTool.setHset(key, data, function (err, result) {
            if (err) {
                console.log('setAccessToken to redis error: ', err);
                callBack({
                    isSuccess: false,
                    err: err
                })
                return;
            } else {
                callBack({
                    isSuccess: true,
                    result: result
                });
                console.log({
                    isSuccess: true,
                    result: result
                });
                // logger.info({
                //     message: 'access_token update',
                //     sql: "save accessToken to redis"
                // });
            }
        });
    },
    getAccessToken: function (callBack) {
        redisTool.getHgetall(key, toUTF8(function (err, result) {
            if (err) {
                console.log('Error: get connection from pool in getAccessToken: ', err);
                callBack({
                    isSuccess: false
                });
                return;
            } else {
                if (result) {
                    callBack({
                        isSuccess: true,
                        rows: [result]
                    })
                } else {
                    callBack({
                        isSuccess: false
                    });
                }

            }

        }))
    }
};